<template>
  <div id="register">
    <el-form
      v-loading.fullscreen.lock="loading"
      element-loading-text="等待注册结果ing..."
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="80px"
      class="demo-ruleForm"
    >
      <el-form-item label="序列号" prop="serial_number">
        <el-input type="text" v-model="ruleForm.serial_number" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input type="text" v-model="ruleForm.account" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="重复密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="pic_code" label-width="160px">
        <img :src="pic_code_url" alt="加载失败" class="p_code" @click="re_pic_code" />
        <el-input type="text" v-model="ruleForm.pic_code" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "register",
  data() {
    var validate_serial_number = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入序列号"));
      } else if (value.length != 10) {
        callback(new Error("序列号应该为10位"));
      } else {
        callback();
      }
    };
    var validate_account = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else if (value.length < 6 || value.length > 12) {
        callback(new Error("密码应为6~12位！"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var validate_pic_code = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入验证码"));
      } else if (value.length !== 4) {
        callback(new Error("验证码为4位!"));
      } else {
        callback();
      }
    };
    return {
      pic_code_url: "http://127.0.0.1/school_big/api/yzm.jfif",
      ruleForm: {
        serial_number: "",
        account: "",
        pass: "",
        checkPass: "",
        pic_code: ""
      },
      rules: {
        serial_number: [{ validator: validate_serial_number, trigger: "blur" }],
        account: [{ validator: validate_account, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        pic_code: [{ validator: validate_pic_code, trigger: "blur" }]
      }
    };
  },
  methods: {
    //注册
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          alert("register注册有问题!!");
          return false;
        }
      });
    },
    re_pic_code() {
      alert("register重新获取验证码");
    }
  }
};
</script>

<style lang="less">
#register {
  padding: 0px 20px 10px;
  .el-input__suffix {
    color: rgb(104, 241, 62);
  }
  .el-form-item__content {
    position: relative;
    > .p_code {
      position: absolute;
      cursor: pointer;
      top: 4px;
      left: -160px;
      width: 100px;
      height: 36px;
    }
    .el-button {
      padding: 12px 70px;
      transform: translateX(-10%);
      font-size: 18px;
      border-radius: 8px;
      font-family: "楷体";
    }
  }
}
</style>